package Demo06.Demo05;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;



public class Demo06_Test5 {
    public static void main(String[] args) {
        File file=new File("G:\\myFiles");
        File file2=new File("G:\\log.txt");
        if(!file2.exists()){
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        remove(file, file2);
    }
    public static void remove(File file,File txt){
        if(file.isFile()){
            boolean isSuccess=file.delete();
            long l=System.currentTimeMillis();
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String string=format.format(l);
            FileOutputStream fos=null;
            try {
                fos=new FileOutputStream(txt,true);
                String pk=isSuccess?"删除成功":"删除失败";
                String string2=file.getName()+pk+" "+string;
                System.out.println(string2);
                fos.write(string2.getBytes());
                fos.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(fos!=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }else {
            File[] files=file.listFiles();
            for (File file2 : files) {
                remove(file2,txt);
            }
        }
    }
}
